package com.ruoyi.api.web;

import com.ruoyi.api.base.BaseApi;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.weixin.domain.Address;
import com.ruoyi.weixin.domain.Client;
import com.ruoyi.weixin.service.IAddressService;
import com.ruoyi.weixin.service.IClientService;
import com.ruoyi.weixin.service.IWxService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@CrossOrigin
@RestController
@RequestMapping("/api/weixin")
public class WeixinApi extends BaseApi {
    @Autowired
    private IWxService wxService;

    @Autowired
    private IAddressService addressService;

    @Autowired
    private IClientService clientService;

    @PostMapping("/login")
    public AjaxResult login(String code, String encryptedData, String iv) throws WxErrorException {
        Client client = wxService.login(code, encryptedData, iv);
        AjaxResult ajaxResult = success("登录成功", client);
        return ajaxResult;
    }

    @PostMapping("/wxPhone")
    public AjaxResult wxPhone(String clientId, String code, String encryptedData, String iv) throws WxErrorException {
        Client client = wxService.wxPhone(clientId, code, encryptedData, iv);
        AjaxResult ajaxResult = success("获取成功", client);
        return ajaxResult;
    }

    /*@PostMapping("/emailVerifi")
    public AjaxResult emailVerifi(String clientId, String email){
        String regStr = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regStr);
        Matcher m = p.matcher(email);
        if (StringUtils.isEmpty(email) || (! m.matches())){
            return error("获取失败");
        }
        wxService.emailVerifi(clientId, email);
        return success("获取成功");
    }

    @PostMapping("/emailBinding")
    public AjaxResult emailBinding(String clientId, String email, String verifi){
        String regStr = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regStr);
        Matcher m = p.matcher(email);
        if (StringUtils.isEmpty(email) || (! m.matches())){
            return error("绑定失败");
        }
        if (wxService.emailBinding(clientId, email, verifi)){
            return success("绑定成功", email);
        }else{
            return error("绑定失败");
        }
    }

    @PostMapping("/clientBirthday")
    public AjaxResult clientBirthday(Client client){
        if (clientService.clientBirthday(client)){
            return success("设置成功");
        }else{
            return error("已设置生日，不能再修改");
        }
    }*/

    /*@PostMapping("/addAddress")
    public AjaxResult addAddress(Address address){
        addressService.insertAddress(address);
        return success("添加成功", address);
    }

    @PostMapping("/editAddress")
    public AjaxResult editAddress(Address address){
        addressService.updateAddress(address);
        return success("修改成功", address);
    }

    @PostMapping("/addressDefault")
    public AjaxResult addressDefault(String clientId, String addressId){
        addressService.addressDefault(clientId, addressId);
        return success("设置成功");
    }

    @PostMapping("/getAddress")
    public AjaxResult getAddress(String clientId){
        List<Address> address = addressService.selectAddressByClientId(clientId);
        return success("获取成功", address);
    }

    @PostMapping("/deleteAddress")
    public AjaxResult deleteAddress(String addressId){
        addressService.deleteAddressById(addressId);
        return success("删除成功");
    }

    @PostMapping("/getAddressById")
    public AjaxResult getAddressById(String addressId){
        return success("获取成功", addressService.selectAddressById(addressId));
    }*/

    @PostMapping("/testHasLogin")
    public AjaxResult testHasLogin(){
        return success("testHasLogin");
    }

    @GetMapping("/testAnon")
    public AjaxResult testAnon(){
        return success("testAnon");
    }
}
